package com.sunriz.motordrive.server.photoThermal.Impl;

import com.sunriz.motordrive.domain.DTO.Result;
import com.sunriz.motordrive.server.common.impl.BaseSerialImpl;
import com.sunriz.motordrive.server.photoThermal.PhotoThermalHc;
import com.sunriz.motordrive.utils.ArrayUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: HcTimedQueryImpl
 * Package: com.sunriz.motordrive.server.impl
 * Description:
 *
 * @Author BuTang
 * @Create 2024/5/23 10:31
 * @Version 1.0
 */
@Slf4j
@Service
public class PhotoThermalHcImpl implements PhotoThermalHc {

    /**
     * Key是寄存器地址，Value是寄存器数量
     */
    public static Map<Integer,Integer> mapOnlyRead = new HashMap<>();
    public static Map<Integer,Integer> mapReadAndWrite = new HashMap<>();


//    三十二位寄存器点位
    public static ArrayList byte32 = new ArrayList();

//    每一个三十二位的寄存器点位
    public static ArrayList eachAddrByte32 = new ArrayList();

    public static Map<Integer,Integer> byte32Map = new HashMap();

    static {
        mapOnlyRead.put(2001,125);
        mapOnlyRead.put(2126,1);
        mapOnlyRead.put(2600,26);

        mapReadAndWrite.put(2400,5);
        mapReadAndWrite.put(2408,21);
        mapReadAndWrite.put(2450,8);
        mapReadAndWrite.put(2500,10);
        mapReadAndWrite.put(2626,96);
        mapReadAndWrite.put(2722,100);


        byte32.add(2600);
        byte32.add(2626);
        byte32.add(2722);
        byte32.add(2450);

        for (int i = 2600; i < 2624; i+=2) {
            eachAddrByte32.add(i);
        }
        for (int i = 2626; i < 2722; i+=2) {
            eachAddrByte32.add(i);
        }
        for (int i = 2722; i < 2822; i+=2) {
            eachAddrByte32.add(i);
        }
        for (int i = 2450; i < 2458; i+=2) {
            eachAddrByte32.add(i);
        }
    }


    @Autowired
    BaseSerialImpl serialPortImpl;

    @Override
    public Result getOnlyReadData() {
        jssc.SerialPort myPort = BaseSerialImpl.serialPort;
        // 判断端口是否开启，若开启，则开始定时查询数据
        if (myPort != null) {
            if (myPort.isOpened() == true ) {
                mapOnlyRead.forEach((integer, integer2) -> serialPortImpl.sendModbus(ArrayUtils.fillIn0304Msg(1,3,integer,integer2)));
            }else {
                log.info("端口尚未开启，无法读取数据");
            }
        }else {
            log.info("端口尚未开启，无法读取数据");
        }
        return Result.success(BaseSerialImpl.returnRead);
    }


    @Override
    public Result getReadAndWrite() {
        jssc.SerialPort myPort = BaseSerialImpl.serialPort;
        // 判断端口是否开启，若开启，则开始定时查询数据
        if (myPort != null) {
            if (myPort.isOpened() == true ) {
                mapReadAndWrite.forEach((integer, integer2) -> serialPortImpl.sendModbus(ArrayUtils.fillIn0304Msg(1,3,integer,integer2)));
            }else {
                log.info("端口尚未开启，无法读取数据");
            }
        }else {
            log.info("端口尚未开启，无法读取数据");
        }
        return Result.success(BaseSerialImpl.returnReadAndWrite);
    }
}
